<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit"
	 :showHistoryBtn="true">
		<om-autoform @deleteItem="deleteUserItem" ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>
<script>
	import {
		getFormFields,
		buildFormData,
		formValidation,
		changeEditor,
		formShowLoading,
		configFormEditor,
		getWorkCacheKey,
		setSubmitSubValue,
		changeConfigAttr,
		configSelectData,
		formModelLoad
	} from '@/static/js/formUtil.js';
	import dateTimePicker from '@/static/js/dateTimePicker.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from '@/static/js/moment.js'
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				initValue: {
					user1OffName: '',
					user2OffName: ''
				},
				user1CacheKey: 'adm-meet-summary-user1-cache',
				user2CacheKey: 'adm-meet-summary-user2-cache',
				user3CacheKey: 'adm-meet-summary-user3-cache',
				user4CacheKey: 'adm-meet-summary-user4-cache',
				user5CacheKey: 'adm-meet-summary-user5-cache',
				boardroomCacheKey: 'adm-meet-summary-boardroom-cache',
				defaultDataCache1:'adm-meet-summary-user4Default-cache',
				defaultDataCache2:'adm-meet-summary-user3Default-cache',
				user2Id:''
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			getWorkParam(){
				let _that = this
				return {
					busCode:_that.busCode,
					user2OffId:_that.user2Id
				}
			},
			deleteUserItem(e){
				if(e == 'user3Id'){
				let _that = this,
					list=[],
					user5IdLsit = [],
					user5NameLsit = [],
					list2=[]     
				let userIds =_that.$refs.autoform.formdata.user3Id.split(',')
				let userNames = _that.$refs.autoform.formdata.user3Name.split(',')
				let user5Ids =_that.$refs.autoform.formdata.user5Id.split(',')
				let user5Names = _that.$refs.autoform.formdata.user5Name.split(',')
				for(let userIndex in userIds){
					let obj = {
						id:userIds[userIndex],
						username:userNames[userIndex],
					}
					list.push(obj)
					for(let user5Index in user5Ids){
						if(userIds[userIndex] == user5Ids[user5Index]){
							user5IdLsit.push(user5Ids[user5Index])
							user5NameLsit.push(user5Names[user5Index])
						}
					}
				}
				_that.$refs.autoform.formdata.user5Id = user5IdLsit.map(e=>e).join(',')
				_that.$refs.autoform.formdata.user5Name = user5NameLsit.map(e=>e).join(',')
				let selectIdList =  _that.$refs.autoform.formdata.user5Id.split(',')
				let selectNameList =  _that.$refs.autoform.formdata.user5Name.split(',')
				for(let index in selectIdList){
					let obj2 = {
						id:userIds[index],
						username:userNames[index],
					}
					list2.push(obj2)
				}
				_that.$refs.autoform.changeEditor('title2', 'user5Id', {
					initSelect:list2
				})
					_that.onSelectUser3(list)
				}
			},
			clearUser3(){
				let _that = this
				_that.$refs.autoform.clear("title2","user3Id")
				_that.$refs.autoform.clear("title2","user5Id")
			},
			onSelectStartTime(){
				let _that = this
				if(_that.$refs.autoform.formdata.startTime){
					let sT =moment( _that.$refs.autoform.formdata.startTime,'hh:mmm')
					let eT =moment( _that.$refs.autoform.formdata.endTime,'hh:mmm')
					let dT = eT.diff(sT,'minute')
					let hh = Math.floor(dT/60)
					let mm = dT%60
					if(hh<1){
						_that.$refs.autoform.formdata.time = mm + '分钟'
					}else{
						_that.$refs.autoform.formdata.time = hh + '小时' + mm + '分钟'
					}
				}
			},
			onSelectEndTime(){
				let _that = this
				if(!_that.$refs.autoform.formdata.startTime){
					_that.om.toast('请选择开始时间',3000)
					return
				}
				let sT =moment( _that.$refs.autoform.formdata.startTime,'hh:mmm')
				let eT =moment( _that.$refs.autoform.formdata.endTime,'hh:mmm')
				let dT = eT.diff(sT,'minute')
				let hh = Math.floor(dT/60)
				let mm = dT%60
				if(hh<1){
					_that.$refs.autoform.formdata.time = mm + '分钟'
				}else{
					_that.$refs.autoform.formdata.time = hh + '小时' + mm + '分钟'
				}
			},
			verifyUser5() {
				if (!this.formdata.user3Id) {
					let title  = this.$refs.autoform.getEditorTitle('title2','user3Id')
					return "请先选择"+title
				}
				return true
			},
			onSelectUser3(item) {
				let _that = this;
				if (item.length) {
					var list = item.map(e => {
						return {
							id: e.id,
							username: e.username,
							userPosInstName: e.userPosInstName?e.userPosInstName:''
						}
					})
					_that.$refs.autoform.changeEditor('title2', 'user5Id', {
						linkParam: {
							namefield: "username",
							searchfield: "q",
							initdata: JSON.stringify(list),
							sign:'id',
							cachename: _that.user5CacheKey,
							placeholder: urlUtil.userPageUrl().prompt
						}
					});
				}
			},
			formSubmit() {
				let _that = this
				_that.formdata.updateBottomFileFlag= true
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				if(Object.prototype.toString.call(_that.$refs.autoform.formdata.bottomFileList) == "[object String]"){
					_that.formdata.bottomFileListStr = _that.$refs.autoform.formdata.bottomFileList|| ''
				}else{
					_that.formdata.bottomFileListStr = JSON.stringify(_that.$refs.autoform.formdata.bottomFileList || []) || ''
				}
				_that.om.formSave('tmeetingSummary/addTmeetingSummary', _that.formdata, _that);
			},
			initLoad(options) {
				let _that = this;
				var ajaxArr = [
					urlUtil.zposUrl(options.code), //制表岗位
					urlUtil.sysdictComboboxUrl('36285'), //会议类别
					// urlUtil.sysdictComboboxUrl('37967'), //会议地点
					{
						url: "regularMeetingList/getList" //例会名称
					},
					urlUtil.dataDictInitValueUrl('DD021017003'), //分数
				];
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push(
						{
							url:'tmeetingSummary/getInfo',
							param:{
									id:fileId
								},
							method:'post'
						}
					)
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', ajaxArr[0].namefield), mainData);
					changeEditor('title1', 'cateId', 'data', configSelectData(res[1], 'id', ajaxArr[1].namefield), mainData);
					changeEditor('title1', 'boardroomName', { //会议地点
						linkParam: {
							other: JSON.stringify({
								code: '37967'
							}),
							initload: true,
							namefield: urlUtil.sysdictComboboxUrl().namefield,
							url: urlUtil.sysdictComboboxUrl().url,
							cachename: _that.boardroomCacheKey,
						}
					}, null, mainData);
					changeEditor('title1', 'meetingName', 'data', configSelectData(res[2], 'id', 'cateName'), mainData);
					changeEditor('title2', 'user1OffId', {
						'link':'../../public/pageSearch/pageSearch',
						linkParam: {
							valuefield: 'id',
							namefield: urlUtil.officePageUrl().namefield,
							searchfield: 'q',
							url: urlUtil.officePageUrl().url,
							cachename: _that.user1CacheKey,
							placeholder: urlUtil.officePageUrl().prompt
						}
					}, null, mainData);
					changeEditor('title2', 'user2OffId', {
						'link':'../../public/pageSearch/pageSearch',
						linkParam: {
							namefield: urlUtil.officePageUrl().namefield,
							searchfield: 'q',
							url: urlUtil.officePageUrl().url,
							cachename: _that.user2CacheKey,
							placeholder: urlUtil.officePageUrl().prompt
						}
					}, null, mainData);
					changeEditor('title2', 'user3Id','isSearchUser', true, mainData);
					changeEditor('title2', 'user3Id', {
						'link':'../../public/searchUser/searchUser',
						linkParam: {
							namefield: urlUtil.userPageUrl().namefield,
							searchfield: "q",
							cachename: _that.user3CacheKey,
							defaultDataCache:_that.defaultDataCache2,
							isNew:true,
							defaultData:'searchUserDefaultData',
							placeholder: urlUtil.userPageUrl().prompt,
							pagination: true
						}
					}, null, mainData);
					changeEditor('title2', 'user4Id','isSearchUser', true, mainData);
					changeEditor('title2', 'user4Id', {
						'link':'../../public/searchUser/searchUser',
						linkParam: {
							namefield: urlUtil.userPageUrl().namefield,
							searchfield: "q",
							defaultDataCache:_that.defaultDataCache1,
							cachename: _that.user4CacheKey,
							isNew:true,
							placeholder: urlUtil.userPageUrl().prompt,
							pagination: true
						}
					}, null, mainData);
					changeEditor('title5', 'score', {
						defaultValue: res[3].value,
						max: res[3].value
					}, null, _that.subConfigData['title5']);
					if (_that.subEditorObj['title5']) {
						changeEditor('title5', 'works', {
							getWorkParam:'getWorkParam',
							titleTemplate: '工作项：<%= workName %>',
							subtitle2Template: '效果：<%= workOutput %>',
							subtitle3Template: '计划<%= planEndDate %>完成'
						}, null, mainData);
					}
					let tempFileList = []
					let fileUrlList = []
					changeEditor('bottominfo','bottomFileUpload','fileUploadKey',"90",mainData)
					changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
					changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for(let index in res[4].value.tmeetingSummary){
							if(index == 'user2Id'){
								_that.user2Id = res[4].value.tmeetingSummary[index]
							}
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[4].value.tmeetingSummary[index])
								_that.$set(_that.formdata,index,res[4].value.tmeetingSummary[index])
							}
						}
						if(_that.formdata.bottomFileList){
							let tempFileList = []
							let fileUrlList = _that.formdata.bottomFileList.map(t=>{
								tempFileList.push(t.fileSaveName)
								return t.fileSaveName
							})
							changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
							changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
						}
						let workList = _that._.cloneDeep(res[4].value.tmeetingSummary_works)
						_that.subEditorObj['title5'].workList = workList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'works',JSON.stringify(workList))
						_that.$set(_that.formdata,'works',JSON.stringify(workList))
						delete res[4].value.workList
						
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex('topinfo','zposId')
							_that.$refs.autoform.initSelectIndex('title1','cateId')
						})
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}
						if(!_that.$refs.autoform.formdata.meetingDate){
							_that.$refs.autoform.formdata.meetingDate = moment().format('YYYY-MM-DD')
						}
						if(_that.$refs.autoform.formdata.user3Id){
							let list = []
							let list2 = []
							let userIds =_that.$refs.autoform.formdata.user3Id.split(',')
							let userNames = _that.$refs.autoform.formdata.user3Name.split(',')
							for(let userIndex in userIds){
								let obj = {
									id:userIds[userIndex],
									username:userNames[userIndex],
								}
								list.push(obj)
							}
							let user5Ids =_that.$refs.autoform.formdata.user5Id.split(',')
							let user5Names = _that.$refs.autoform.formdata.user5Name.split(',')
							for(let user5Index in user5Ids){
								let obj = {
									id:user5Ids[user5Index],
									username:user5Names[user5Index],
								}
								list2.push(obj)
							}
							_that.$refs.autoform.changeEditor('title2', 'user5Id', {
								initSelect:list2
							})
							_that.onSelectUser3(list)
						}
					})
				}, options);
			}
		},
		onLoad(options) {
			let _that = this;
			_that.initLoad(options);
		},
		onUnload(){
			let _that = this
			uni.removeStorage({
				key:_that.defaultDataCache2
			})
			uni.removeStorage({
				key:_that.defaultDataCache1
			})
		//#ifdef APP-PLUS
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onShow() {
			uni.hideLoading()
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key: _that.user1CacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.user1OffId = d.id;
							formdata.user1Name = d.userName
							uni.removeStorage({
								key: _that.user1CacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.user2CacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.user2OffId = d.id;
							formdata.user2Name = d.userName
							_that.user2Id = d.userId
							uni.removeStorage({
								key: _that.user2CacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.user3CacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							uni.setStorage({
								key:_that.defaultDataCache2,
								data:d
							})
							var formdata = _that.$refs.autoform.formdata;
							if(d.length){
								let idList = []
								let nameList = []
								let user3Id = ''
								for(let index in d){
									idList.push(d[index].id)
									nameList.push(d[index].username)
								}
								
								let idStr = idList.map(e =>e).join(",")
								let nameStr = nameList.map(e =>e).join(",");
								
								formdata.user3Id = idStr
								formdata.user3Name = nameStr
								
							}
							uni.removeStorage({
								key: _that.user3CacheKey
							});
							let userList = []
							let userIds =_that.$refs.autoform.formdata.user3Id.split(',')
							let userNames = _that.$refs.autoform.formdata.user3Name.split(',')
							for(let userIndex in userIds){
								let obj = {
									id:userIds[userIndex],
									username:userNames[userIndex],
								}
								userList.push(obj)
							}
							_that.onSelectUser3(userList)
						}
					}
				});
				uni.getStorage({
					key: _that.user4CacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							uni.setStorage({
								key:_that.defaultDataCache1,
								data:d
							})
							var formdata = _that.$refs.autoform.formdata;
							let idList = []
							let nameList = []
							let user4Id = ''
							if(formdata.user4Id){
								user4Id = formdata.user4Id.split(',')
							}
							for(let index in d){
								idList.push(d[index].id)
								nameList.push(d[index].username)
							}
							let idStr = idList.map(e =>e).join(",")
							let nameStr = nameList.map(e =>e).join(",");
							formdata.user4Id = idStr
							formdata.user4Name = nameStr
							
							uni.removeStorage({
								key: _that.user4CacheKey
							});
							
							let userList = []
							let userIds =_that.$refs.autoform.formdata.user4Id.split(',')
							let userNames = _that.$refs.autoform.formdata.user4Name.split(',')
						}
					}
				});
				uni.getStorage({
					key: _that.user5CacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.user5Id = d.map(e => e.id).join(",")
							formdata.user5Name = d.map(e => e.username).join(",");
							_that.$refs.autoform.changeEditor('title2', 'user5Id', {
								initSelect: d
							})
							uni.removeStorage({
								key: _that.user5CacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.boardroomCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							if(Object.prototype.toString.call(d) != '[object Object]'){
								formdata.boardroomName = d
							}else{
								formdata.boardroomId = d.id
								formdata.boardroomName = d.dicname
							}
							uni.removeStorage({
								key: _that.boardroomCacheKey
							});
						}
					}
				});
			}
		}
	};
</script>
<style></style>
